package com.shanling.shanlingcontroller.ui.fragment;

import android.content.ContentUris;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.base.BaseLazyFragment;
import com.shanling.shanlingcontroller.bean.Constants;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.bean.Music;
import com.shanling.shanlingcontroller.music.event.MetaChangedEvent;
import com.shanling.shanlingcontroller.music.player.PlayManager;
import com.shanling.shanlingcontroller.utils.FormatUtil;

import org.w3c.dom.Text;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by ch1 on 2019/11/15.
 */

public class MusicInformationFragment extends BaseLazyFragment {
    @BindView(R.id.tv_ablum)
    TextView tvAblum;
    @BindView(R.id.tv_style)
    TextView tvStyle;
    @BindView(R.id.tv_size)
    TextView tvSize;
    @BindView(R.id.tv_age)
    TextView tvAge;
    @BindView(R.id.tv_coding)
    TextView tvCoding;
    @BindView(R.id.tv_Bit_depth)
    TextView tvBitDepth;
    @BindView(R.id.tv_Bit_rate)
    TextView tvBitRate;
    @BindView(R.id.tv_Sampling_Rate)
    TextView tvSamplingRate;
    @BindView(R.id.tv_postion)
    TextView tvPostion;
    @Override
    protected void onLazyLoad() {

    }
    private void setSongInforMation(Music music) {
        if(music!=null){
            if(TextUtils.isEmpty(music.getAlbum())||music.getAlbum().equals("<unknown>")){
                tvAblum.setText(getString(R.string.album)+": "+getString(R.string.unknown));
            }else {
                tvAblum.setText(getString(R.string.album)+": "+music.getAlbum());
            }
            if(TextUtils.isEmpty(music.getYear())){
                tvAge.setText(getString(R.string.age)+": "+getString(R.string.unknown));
            }else {
                tvAge.setText(getString(R.string.age)+": "+music.getYear());
            }
            tvSize.setText(getString(R.string.size)+": "+ FormatUtil.INSTANCE.formatSize(music.getFileSize()));
            tvPostion.setText(getString(R.string.position)+": "+music.getUri());
        }else {
             tvAblum.setText(getString(R.string.album)+": "+getString(R.string.unknown));
             tvStyle.setText(getString(R.string.style)+": "+getString(R.string.unknown));
            tvSize.setText(getString(R.string.size)+": "+getString(R.string.unknown));
            tvAge.setText(getString(R.string.age)+": "+getString(R.string.unknown));
            tvCoding.setText(getString(R.string.coding)+": "+getString(R.string.unknown));
           tvBitDepth.setText(getString(R.string.Bit_depth)+": "+getString(R.string.unknown));
            tvBitRate.setText(getString(R.string.Bit_rate)+": "+getString(R.string.unknown));
           tvSamplingRate.setText(getString(R.string.Sampling_Rate)+": "+getString(R.string.unknown));
            tvPostion.setText(getString(R.string.position)+": "+getString(R.string.unknown));
        }
    }
    @Override
    protected void initViewsAndEvents() {
        setSongInforMation(PlayManager.getPlayingMusic());


    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_musicinformation;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return true;
    }
    @Override
    protected void onEventComing(EventCenter eventCenter) {
        switch (eventCenter.getEventCode()) {
            case  Constants.METACHANGE:
                MetaChangedEvent eventCenterData = (MetaChangedEvent) eventCenter.getData();
                setSongInforMation(eventCenterData.getMusic());
                break;
        }
    }





}
